home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17183 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  35 lines

  1. Path: news.th-darmstadt.de!news
  2. From: enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Calling the wrong constructor
  5. Date: 14 Apr 1996 10:08:42 +0200
  6. Organization: Fachbereich Informatik, TH Darmstadt
  7. Sender: enno@kitz.inferenzsysteme.informatik.th-darmstadt.de
  8. Message-ID: <ltn34fns6s.fsf@kitz.inferenzsysteme.informatik.th-darmstadt.de>
  9. References: <4kot87$796@earth.njcc.com>
  10. NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
  11. In-reply-to: mike@pluto.njcc.com's message of 13 Apr 1996 18:53:59 GMT
  12. X-Newsreader: Gnus v5.1
  13.  
  14.  
  15.      This might seem like a basic question, but lets say you something set u 
  16.    like the following:
  17.  
  18.    class Parent { Parent() { allocsomething} ~Parent() { deletesomething }... };
  19.    class Foo : Parent { Foo() { allocsomething } ~Foo() {deletesomething }... };
  20.    class Contain { Parent *ptr; ... };
  21.  
  22.    both parent, and foo allocate memory, and will free it up when being 
  23.    destructed.  Contain just holds a pointer of type Parent, and its 
  24.    constructor takes such a pointer as a parameter.
  25.      If I store a pointer of type Foo into contain.ptr and discard that 
  26.    pointer.  Is there any way to have contain (via destructors) free up all 
  27.    memory allocated by Foo and/or Parent?
  28.  
  29. Declare the dtor of 'Parent' as virtual.
  30. Otherwise deleting a subclass-object of 'Parent' through
  31. a pointer to 'Parent' will lead to undefined behavior.
  32.  
  33.         Enno
  34.  
  35.